var app = getApp();
var utils = require("../../utils/util.js");
var req = require("../../utils/req.js");
var url = require("../../utils/url.js");
Page({

  data: {
    loginShow: false,
    address: '获取地址',
    userInfo: null,
    refreshState: true,
  },

  onShow: function() {
    var that = this;
    that.setData({
      refreshState: app.globalData.refreshState
    })
    if (app.globalData.isNewUserInfo) {
      app.globalData.isNewUserInfo = false;
      that.onUserLogin();
      return;
    }
    var userInfo = app.globalData.userInfo;
    that.setData({
      userInfo: userInfo
    })
    return;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    that.onUserLogin();
    that.setData({
      refreshState: true,
    })
  },

  onUserLogin: function() {
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var data = {
          code: res.code,
        };

        req.userRequest("请稍等", "POST", url.user['login'], data, function back(res) {
          wx.stopPullDownRefresh();
          console.log(res);
          app.globalData.userInfo = res.data;
          app.globalData.userId = res.data.id;
          app.globalData.userToken = res.msg;
          that.setData({
            userInfo: res.data,
          })
          var nickname = res.data.nickname;
          if (nickname != null && nickname != undefined && nickname != "") {
            that.setData({
              loginShow: true,
            })
          } else {
            that.setData({
              loginShow: false,
            })
          }
        });
        var address = app.globalData.address;
        if (address != null && address != undefined && undefined != "") {
          that.setData({
            address: address
          })
        }
      }
    })
  },

  // 获取用户信息
  onGotUserInfo: function(e) {
    var that = this;
    const data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
    };
    that.getLocation();
    req.userRequest("请稍等", "POST", url.user['info'], data, function back(res) {
      app.globalData.userInfo = res.data;
      that.setData({
        loginShow: 'true',
        userInfo: res.data
      })
    });
  },

  bindGetPhone: function(e) {
    var that = this;
    const data = {
      uid: app.globalData.userInfo.userId, //给传输参数带上 uid
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
    }
    req.userRequest("请稍等", 'POST', url.user['phone'], data, function back(res) {
      app.globalData.userInfo = res.data;
      wx.showModal({
        content: '授权成功！',
      })
      that.setData({
        userInfo: res.data
      })
    })
  },

  // 跳转充值
  goToRecharge: function() {
    wx.navigateTo({
      url: '../walletRecharge/walletRecharge',
    })
  },
  // 跳转消费明细
  goToOrder: function() {
    wx.navigateTo({
      url: '../order/order',
    })
  },
  /**
   * 地图
   */
  getLocation: function() {
    var that = this;
    utils.getLocation((res) => {
      console.log(res);
      app.globalData.location = res.location;
      app.globalData.address = res.address_component.street
      console.log(app.globalData.latitude)
      var address = res.address_component.street;
      that.setData({
        address: address
      })
    });
  },
  goToStore: function() {
    var that = this;
    if (app.globalData.location.length == 0) {
      wx.showModal({
        title: '提示',
        content: '我需要您提供定位信息',
        success: function(res) {
          if (res.confirm) {
            that.getLocation();
            wx.navigateTo({
              url: '../store/store',
            })
          } else if (res.cancel) {
            wx.navigateTo({
              url: '../store/store',
            })
          }
        },
      })
    } else {
      that.getLocation();
      wx.navigateTo({
        url: '../store/store',
      })
    }
  },
  /**
   * 跳转商家
   */
  gotoMe: function() {
    wx.navigateTo({
      url: '../storeList/storeList',
    })
  },
  // 跳转余额
  goToWallet: function() {
    wx.navigateTo({
      url: '../wallet/wallet',
    })
  },
  // 跳转积分
  goToIntegral: function() {
    wx.navigateTo({
      url: '../integral/integral',
    })
  },

  // 扫码支付
  weChatPay: function() {
    wx.scanCode({
      success: (res) => {
        console.log(res)
        var data = {
          orderNumber: res.result
        }
        req.userRequest("请稍等", 'POST', url.user['userScanQrcoderGetOrderInfo'], data, function back(res) {
          if (res.data == null) {
            wx.showToast({
              title: '订单不存在',
            })
          } else {
            wx.navigateTo({
              url: '/pages/user_wechat_pay/user_wechat_pay?orderNumber=' + res.data.orderNumber + "&price=" + res.data.price + "&createTime=" + res.data.createTime
            })
          }
        })
      }
    })
  },

  callPhone: function() {
    wx.makePhoneCall({
      phoneNumber: '4001161680',
      success: function() {
        console.log('拨打成功')
      },
      fail: function() {
        console.log('拨打失败')
      }
    })
  },
  help: function() {
    wx.showModal({
      showCancel: false,
      content: '此功能暂未开放',
    })
  }
})